package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.mapper.CategoryMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired(required = false)
    private CategoryMapper categoryMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 清理缓存数据
     * @param pattern
     */
    private void cleanCache(String pattern){
        Set keys = stringRedisTemplate.keys(pattern);
        stringRedisTemplate.delete(keys);
    }


    /**
     * 根据类型查询分类
     *
     * @param type
     * @return
     */
    @Override
    public List<Category> list(Integer type) {
        //非空判断

        List<Category> categoryList = categoryMapper.list(type);
        return categoryList;
    }

    /**
     * 启用禁用分类
     *
     * @param status
     * @param id
     * @return
     */
    @Transactional
    @Override
    public Result startOrClose(Integer status, Long id) {
        //非空判断
        if (id == null) {
            return Result.error(MessageConstant.ID_NOT_FOUND);
        }
        if (status == null) {
            return Result.error(MessageConstant.UNKNOWN_ERROR);
        }
        Category category = Category.builder()
                .status(status)
                .id(id)
                .build();
        categoryMapper.update(category);

        //将所有的菜品缓存数据清理掉，所有以dish_开头的key
        cleanCache("category_*");
        return Result.success();
    }

    /**
     * 修改分类
     *
     * @param categoryDTO
     * @return
     */
    @Transactional
    @Override
    public Result update(CategoryDTO categoryDTO) {
        //非空判断
        if (categoryDTO.getId() == null) {
            return Result.error(MessageConstant.ID_NOT_FOUND);
        }

        //1.对象属性拷贝，categoryDTO 到 category
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);

        //2.补全信息
        //2.1修改时间
        //category.setUpdateTime(LocalDateTime.now());

        //2.2修改人
        //category.setUpdateUser(BaseContext.getCurrentId());

        //3.调用dao层
        categoryMapper.update(category);
        //将所有的菜品缓存数据清理掉，所有以dish_开头的key
        cleanCache("category_*");
        return Result.success();
    }

    /**
     * 根据id删除分类
     *
     * @param id
     * @return
     */
    @Transactional
    @Override
    public Result deleteById(Long id) {
        //非空判断
        if (id == null) {
            return Result.error(MessageConstant.ID_NOT_FOUND);
        }
        categoryMapper.deleteById(id);

        //将所有的菜品缓存数据清理掉，所有以dish_开头的key
        cleanCache("category_*");
        return Result.success();
    }

    /**
     * 分类分页查询
     *
     * @param categoryPageQueryDTO
     * @return
     */
    @Override
    public Result page(CategoryPageQueryDTO categoryPageQueryDTO) {
        //若为null，设置默认
        if (categoryPageQueryDTO.getPage() == null) {
            categoryPageQueryDTO.setPage(0);
        }
        if (categoryPageQueryDTO.getPageSize() == null) {
            categoryPageQueryDTO.setPageSize(10);
        }

        PageHelper.startPage(categoryPageQueryDTO.getPage(), categoryPageQueryDTO.getPageSize());

        Page<Category> page = categoryMapper.page(categoryPageQueryDTO);

        PageResult pageResult = new PageResult(page.getTotal(), page.getResult());

        return Result.success(pageResult);

    }

    /**
     * 新增分类
     *
     * @param categoryDTO
     * @return
     */
    @Transactional
    @Override
    public Result add(CategoryDTO categoryDTO) {


        //1.对象属性拷贝，categoryDTO 到 category
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);

        //2.补全信息
        //2.1设置状态默认禁用
        category.setStatus(StatusConstant.DISABLE);

        //2.2创建和修改时间
        //category.setCreateTime(LocalDateTime.now());
        //category.setUpdateTime(LocalDateTime.now());

        //2.3创建和修改人
        //category.setCreateUser(BaseContext.getCurrentId());
        //category.setUpdateUser(BaseContext.getCurrentId());

        //3.调用dao层
        categoryMapper.add(category);

        //清理缓存数据
        String key = "category_" + categoryDTO.getId();
        cleanCache(key);
        return Result.success();
    }
}
